#include <QMessageBox>
#include <QFileDialog>
#include "DialogNewTexture.hpp"
#include "ui_DialogNewTexture.h"

DialogNewTexture::DialogNewTexture(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogNewTexture),
    _isLoaded(false)
{
    ui->setupUi(this);
    initWidgets();
}

DialogNewTexture::~DialogNewTexture()
{
    delete ui;
}

void    DialogNewTexture::initWidgets()
{
    QObject::connect(ui->pushButtonBrowse, SIGNAL(clicked()), this, SLOT(buttonActionBrowse()));
    QObject::connect(ui->pushButtonLoad, SIGNAL(clicked()), this, SLOT(buttonActionLoad()));
    QObject::connect(ui->pushButtonCancel, SIGNAL(clicked()), this, SLOT(buttonActionCancel()));
}

void    DialogNewTexture::buttonActionBrowse()
{
    QString	result = QFileDialog::getOpenFileName(this, QString("Load texture..."), QString("."), QString("Image (*.png *.jpg *.bmp)"));

    if (result != "")
    {
        ui->lineEditTexturePath->setText(result);
    }
}

void    DialogNewTexture::buttonActionLoad()
{
    QString     name = ui->lineEditTextureName->text();

    if (name != "")
    {
        QString path = ui->lineEditTexturePath->text();

        if (path != "")
        {
            _isLoaded = true;
            _name = name.toStdString();
            _path = path.toStdString();
            if (ui->radioButtonExtends->isChecked() == true)
                _textBehaviour = TEXTURE_EXTEND;
            else
                _textBehaviour = TEXTURE_REPEAT;
            this->close();
        }
        else
            QMessageBox::critical(this, "Error", "Please select a file.");
    }
    else
        QMessageBox::critical(this, "Error", "Please enter a texture name.");
}

void                DialogNewTexture::buttonActionCancel()
{
    this->close();
}

std::string         DialogNewTexture::getName() const
{
    return _name;
}

std::string         DialogNewTexture::getPath() const
{
    return _path;
}

TextureBehaviour    DialogNewTexture::getTextBehaviour() const
{
    return _textBehaviour;
}

bool                DialogNewTexture::isLoaded() const
{
    return _isLoaded;
}

